package com.neu.api.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.neu.api.constan.StatusConst;
import com.neu.api.dto.CategoryDTO;
import com.neu.api.exception.BusException;
import com.neu.api.mapper.CategoryMapper;
import com.neu.api.pojo.Category;
import com.neu.api.query.CategoryQuery;
import com.neu.api.service.CategoryService;
import com.neu.api.service.FoodService;
import com.neu.api.utils.PageResult;
import com.neu.api.vo.CategoryVO;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class CategoryServiceImpl implements CategoryService {

    @Resource
    private CategoryMapper categoryMapper;


    @Resource
    FoodService foodService;
    /**
     * 查询菜品
     * @return
     */
    @Override
    public PageResult<CategoryVO> find(CategoryQuery categoryQuery) {

        //设置是否需要分页
        PageHelper.startPage(categoryQuery.getPage(),categoryQuery.getPageSize());  //告诉PageHelper此处需要分页

        com.github.pagehelper.Page<CategoryVO> page = (com.github.pagehelper.Page<CategoryVO>) categoryMapper.find(categoryQuery);

        PageInfo<CategoryVO> pageInfo = page.toPageInfo();
        //查询语句
        //List<CategoryVO> categoryVOList = categoryMapper.find(categoryQuery);

        List<CategoryVO> categoryVOList = pageInfo.getList();

        long total = pageInfo.getTotal();

        PageResult<CategoryVO> pageResult = new PageResult<>();
        pageResult.setTotal(total);
        pageResult.setList(categoryVOList);
        return pageResult;
        //return categoryMapper.find(categoryQuery);我写的
    }

    /**
     * 添加菜品
     * @param categoryDTO
     */
    @Override
    public void add(CategoryDTO categoryDTO) {

        //步骤
            //第一:先把dto--->pojo
            //第二:调用mapper/dao执行insert 语句


        //方式一:将dto-->pojo
        /*
        Category category = new Category();
        category.setName(categoryDTO.getName());
        category.setPassword(categoryDTO.getPassword());
        */

        //方式二:将dto-->pojo
//        Category category = new Category();
//        BeanUtils.copyProperties(categoryDTO,category);

        Category category = new Category();  //name loginName phone
        BeanUtils.copyProperties(categoryDTO,category);
        category.setCreateTime(new Date());
        category.setUpdateTime(new Date());

        category.setStatus(StatusConst.ACTIVE_STATUS);

        categoryMapper.add(category);



    }
    

    @Override
    public void update(Long id, CategoryDTO categoryDTO) {
        Category category = new Category();

        BeanUtils.copyProperties(categoryDTO,category);

        category.setUpdateTime(new Date());
        category.setId(id);
        categoryMapper.update(category);
    }

    @Override
    public void deleteById(long id) {
        //TODO 为什么需要判断当前分类下面是否已经有菜品,如果有需要先删除菜品？
        int count = foodService.findCountByCategoryId(id);
        if(count>0){
            throw new BusException("当前分类下存在菜品");
        }
        categoryMapper.deleteById(id);
    }

    /**
     * 启用禁用账号
     * @param id
     * @param status
     */
    @Override
    public void updateStatus(long id, byte status) {
        Category category = new Category();
        category.setId(id);
        category.setUpdateTime(new Date());
        category.setStatus(status);
        categoryMapper.update(category);
    }

}
